
<template>
  <el-col :span="24" class="bg" @keyup.enter.native="submitForm('formRef')">
    <!-- <el-row :gutter="20" justify="center" align="center"> -->
    <!-- <el-col> -->
    <el-form
      :model="form"
      :rules="rules"
      ref="formRef"
      label-position="left"
      class="demo-ruleForm login-container"
    >
      <h3 class="title">
        <!-- <img style="margin:0 10px;" :src="url"> -->
        商城管理后台
      </h3>
      <el-form-item label="用户名" prop="username" :label-width="formLabelWidth">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" :label-width="formLabelWidth">
        <el-input v-model="form.password" type="password" show-password></el-input>
      </el-form-item>

      <div style="text-align:center; margin-bottom:10px">
        <el-button type="primary" @click="submitForm('formRef')" style="width:60%;" :loading="logining">登录</el-button>
      </div>
    </el-form>
    <!-- </el-col> -->
    <!-- </el-row> -->
  </el-col>
</template>

<script>
export default {
  data() {
    return {
      logining:false,
      form: {
        username: "", //用户名
        password: "" //密码
      },
      formLabelWidth: "80px",
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入用户密码", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {

      this.$refs[formName].validate(valid => {
        if (valid) {
          this.logining = true;
          this.$service
            .login(this.form)
            .then(res => {
              var user = res.data;
              this.logining = false;
              if (res.code == "200") {
                this.$message({
                  message: "登录成功",
                  type: "success"
                });
                localStorage.setItem("isLogin", "isLogin");
                localStorage.setItem("currentUser", JSON.stringify(res.data));
                this.$store.commit("SET_USER", res.data);
                this.$store
                  .dispatch("getCurrentMenu", this.form.username)
                  .then(res => {
                    this.$router.push({ path: "/main" });
                  });
                this.$refs[formName].resetFields();
              } else {
                this.$message({
                  message: res.message,
                  type: "waring"
                });
              }
            })
            .catch(err => {});
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.login-container {
  /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-border-radius: 5px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
  .title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
}
.bg {
  width: 100%;
  height: 100%;

  // background-image: url(../assets/bj.png);
  background: url(../assets/bj.png) no-repeat;
  background-size: 60%;
  background-position: 0% 100%;
}
</style>